package com.example.videodemo.fragment;

import android.content.Context;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.jna.HCNetSDKJNAInstance;
import com.example.videodemo.DemoViewModel;
import com.example.videodemo.R;


public class PlayFragment extends Fragment {
    private DemoViewModel demoViewModel;
    private SurfaceView m_osurfaceView1;
    private int iPreviewID=-1;
    private int position;
    private ProgressBar progressBar;

    public PlayFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        demoViewModel = new ViewModelProvider(this.getActivity()).get(DemoViewModel.class);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_play, container, false);
        Bundle bundle =getArguments();
        position=bundle.getInt("position");
        progressBar = view.findViewById(R.id.loading_progress);
        m_osurfaceView1=view.findViewById(R.id.sfv_play);
        m_osurfaceView1.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                m_osurfaceView1.getHolder().setFormat(PixelFormat.TRANSLUCENT);
                Surface surface = holder.getSurface();
                if (-1 == iPreviewID) {
                    return;
                }
                if (surface.isValid()) {
                    if (-1 == demoViewModel.RealPlaySurfaceChanged_jni(iPreviewID, 0, holder))
                        Toast.makeText(getContext(), "NET_DVR_PlayBackSurfaceChanged" + HCNetSDKJNAInstance.getInstance().NET_DVR_GetLastError(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                if (-1 == iPreviewID) {
                    return;
                }
                if (holder.getSurface().isValid()) {
                    if (-1 == demoViewModel.RealPlaySurfaceChanged_jni(iPreviewID, 0, holder)){
                        Toast.makeText(getContext(), "NET_DVR_PlayBackSurfaceChanged" + HCNetSDKJNAInstance.getInstance().NET_DVR_GetLastError(), Toast.LENGTH_SHORT).show();

                    }
                }
            }
        });
        m_osurfaceView1.setZOrderOnTop(true);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                iPreviewID=demoViewModel.startPlay(m_osurfaceView1, iPreviewID,position);
                if(iPreviewID<0){
                    Toast.makeText(getContext(), "预览失败" + HCNetSDKJNAInstance.getInstance().NET_DVR_GetLastError(), Toast.LENGTH_SHORT).show();
                }else {
                    progressBar.setVisibility(View.GONE);
                }

            }
        }, 1000);




        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

    }
}
